/**
 * 通用工具类
 */
export default class CommonUtils {

    /** 设置该Node，使之具有拦截点击，结束点击冒泡的功能 */
    static setTouchMask(node: cc.Node) {
        if (node) {
            node.on(cc.Node.EventType.TOUCH_START, (e) => {
                e.stopPropagation();
            });
        }
    }

    static randomInt(from: number, to: number): number {
        return Math.floor(from + Math.random() * (to - from));
    }

    static randomFloat(from: number, to: number): number {
        return from + Math.random() * (to - from);
    }

    /** 是否是安装后首次启动 */
    static get isFirstRunGame() {
        let isNotFirstRunGame = localStorage.getItem('NotFirstRunGame');
        localStorage.setItem('NotFirstRunGame', 'true');
        return !isNotFirstRunGame;
    }

    /** 任意坐标系转成Canvas坐标系
     * @param crrSpace 当前点所在是坐标系，即当前物体的父级节点
     * @param position 要转换的坐标
     */
    static convertToCanvasSpace(crrNode: cc.Node) {
        return CommonUtils.convertToTargetSpace(crrNode, cc.Canvas.instance.node);
    }

    /** 任意坐标系转成特定坐标系
     * @param crrSpace 当前点所在是坐标系，即当前物体的父级节点
     * @param position 要转换的坐标
     */
    static convertToTargetSpace(crrNode: cc.Node, targetSpace: cc.Node) {
        let world = crrNode.parent.convertToWorldSpaceAR(crrNode.position);
        let newPos = targetSpace.convertToNodeSpaceAR(world);
        return newPos;
    }

    /** 创建一张1x1白色纹理（始终是黑色的问题未解决）
     */
    static createWhiteSpriteFrame() {
        //TODO:始终是黑色的问题未解决
        let myTexture2D = new cc.Texture2D();
        myTexture2D.initWithData(new DataView(new Uint8Array([255, 255, 255]).buffer, 0, 3), cc.Texture2D.PixelFormat.RGB888, 1, 1);
        let spriteFrame = new cc.SpriteFrame(myTexture2D, new cc.Rect(0, 0, 1, 1));
        return spriteFrame;
    }

}
